package listener;

import javax.swing.undo.UndoManager;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class UndoKeyListener implements KeyListener {

    private UndoManager undoManager;

    public UndoKeyListener(UndoManager undoManager){
        this.undoManager = undoManager;
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_Z) {
            if (undoManager.canUndo()) {
                undoManager.undo();
            }
        }
        if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_Y) {
            if (undoManager.canRedo()) {
                undoManager.redo();
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}
